package com.example.dbcms.controller.user;

import com.example.dbcms.annotation.Log;
import com.example.dbcms.domain.PropertyFee;
import com.example.dbcms.domain.User;
import com.example.dbcms.service.PropertyFeeService;
import com.example.dbcms.service.UserService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
@Api("普通用户物业费用路由")
public class UserPropertyFeeController {

    @Autowired
    private PropertyFeeService propertyFeeService;

    @Autowired
    private UserService userService;

    /**
     * 普通用户查看物业费用
     * @return
     */
    @GetMapping("/residentPayInfo")
    @ApiOperation("普通用户查看物业费用")
    @Log(type="普通用户查看物业费用")
    public String getResidentPayInfo(PropertyFee propertyFee, ModelMap modelMap){
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        propertyFee.setBuildingNo(user.getBuildingNo());
        propertyFee.setRoomNo(user.getRoomNo());
        PageInfo<PropertyFee> propertyFeePageInfo = propertyFeeService.selectAllPropertyFeeInfo(propertyFee);
        modelMap.put("propertyFeeList", propertyFeePageInfo.getList());
        modelMap.put("propertyFee",propertyFee);
        modelMap.put("pageInfo",propertyFeePageInfo);
        return "user/property/index";
    }


    /**
     * 缴纳多条物业费用
     * @param ids
     * @return
     */
    @ApiOperation("缴纳多条物业费用")
    @PostMapping("/payMultiplePropertyFee")
    @ResponseBody
    @Log(type="缴纳多条物业费用")
    public ResultVo payMultiplePropertyFee(@RequestParam(value = "ids") List<Integer> ids){
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        int result = propertyFeeService.payMultiplePropertyFee(ids,user.getUsername());
        if(result==ids.size()){
            return ResultVoUtil.success("缴费成功");
        }
        return ResultVoUtil.error("缴费失败");
    }

    /**
     * 通过id删除该条物业缴费记录
     * @param id
     * @return
     */
    @ApiOperation("通过id缴纳物业费用")
    @GetMapping("/payPropertyFee/{id}")
    @ResponseBody
    @Log(type="通过id缴纳物业费用")
    public ResultVo deletePropertyFee(@PathVariable("id") int id){
        String userAccount = (String) SecurityUtils.getSubject().getPrincipal();//获得账号
        User user = userService.getUserByUserAccount(userAccount);
        int result = propertyFeeService.payPropertyFee(id,user.getUsername());
        if(result==1){
            return ResultVoUtil.success("缴费成功");
        }else if (result==-1){
            return ResultVoUtil.error("请勿重复缴费!");
        }
        return ResultVoUtil.error("缴费失败");
    }
}
